﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Net;
using System.IO;
using System.Text;
using System.Web.Security;
using System.Security.Cryptography;

namespace WebSite
{
    public partial class Alipay_Return : System.Web.UI.Page
    {

        // Methods
        public static string[] BubbleSort(string[] r)
        {
            for (int i = 0; i < r.Length; i++)
            {
                bool flag = false;
                for (int j = r.Length - 2; j >= i; j--)
                {
                    if (string.CompareOrdinal(r[j + 1], r[j]) < 0)
                    {
                        string str = r[j + 1];
                        r[j + 1] = r[j];
                        r[j] = str;
                        flag = true;
                    }
                }
                if (!flag)
                {
                    return r;
                }
            }
            return r;
        }

        public string Get_Http(string a_strUrl, int timeout)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(a_strUrl);
                request.Timeout = timeout;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
                StringBuilder builder = new StringBuilder();
                while (-1 != reader.Peek())
                {
                    builder.Append(reader.ReadLine());
                }
                return builder.ToString();
            }
            catch (Exception exception)
            {
                return ("错误：" + exception.Message);
            }
        }

        public static string GetMD5(string s, string _input_charset)
        {
            byte[] buffer = new MD5CryptoServiceProvider().ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(s));
            StringBuilder builder = new StringBuilder(0x20);
            for (int i = 0; i < buffer.Length; i++)
            {
                builder.Append(buffer[i].ToString("x").PadLeft(2, '0'));
            }
            return builder.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string str = "https://www.alipay.com/cooperate/gateway.do?";
            string str2 = "kuip37avpflqhb94zdbxq79cd6jlacti";
            string str3 = "utf-8";
            string str4 = "2088002029290264";
            str = str + "service=notify_verify&partner=" + str4 + "&notify_id=" + base.Request.QueryString["notify_id"];
            string str5 = this.Get_Http(str, 0x1d4c0);
            string[] strArray2 = BubbleSort(base.Request.QueryString.AllKeys);
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < strArray2.Length; i++)
            {
                if (((base.Request.Form[strArray2[i]] != "") && (strArray2[i] != "sign")) && (strArray2[i] != "sign_type"))
                {
                    if (i == (strArray2.Length - 1))
                    {
                        builder.Append(strArray2[i] + "=" + base.Request.QueryString[strArray2[i]]);
                    }
                    else
                    {
                        builder.Append(strArray2[i] + "=" + base.Request.QueryString[strArray2[i]] + "&");
                    }
                }
            }
            builder.Append(str2);
            string str6 = GetMD5(builder.ToString(), str3);
            string str7 = base.Request.QueryString["sign"];
            if ((str6 == str7) && (str5 == "true"))
            {
                base.Response.Write("success");
            }
            else
            {
                base.Response.Write("fail");
            }
        }

        // Properties
        protected global_asax ApplicationInstance
        {
            get
            {
                return (global_asax)this.Context.ApplicationInstance;
            }
        }

        protected DefaultProfile Profile
        {
            get
            {
                return (DefaultProfile)this.Context.Profile;
            }
        }

    }
}